<?php

use Phalcon\DI\FactoryDefault;
use Phalcon\Mvc\View;
use Phalcon\Crypt;
use Phalcon\Mvc\Dispatcher;
use Phalcon\Mvc\Url as UrlResolver;
use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;
use Phalcon\Mvc\View\Engine\Volt as VoltEngine;
use Phalcon\Mvc\Model\Metadata\Files as MetaDataAdapter;
use Phalcon\Mvc\Model\Metadata\Memory as MemoryMetaDataAdapter;
use Phalcon\Session\Adapter\Files as SessionAdapter;
use Phalcon\Logger\Adapter\File as FileLogger;
use Phalcon\Flash\Direct as Flash;
use Phalcon\Queue\Beanstalk;
use Phalcon\Cache\Frontend\None as FrontendNone;

use Library\Acl\Acl;
use Library\Auth\Auth;
use Library\Mail\Mail;

$di = new FactoryDefault();

/**
 * Register the global configuration as config
 */
$di->set('config', $config);


/**
 * Loading routes from the routes.php file
 */
$di->set('router', function () use ($config) {
    return require __DIR__ . '/routes.php';
});


/**
 * The URL component is used to generate all kind of urls in the application
 */
$di->set('url', function () use ($config) {
    $url = new UrlResolver();
    $url->setBaseUri($config->application->baseUri);

    return $url;
}, true);

/**
 * Database connection is created based in the parameters defined in the configuration file
 */
$di['db'] = function () use ($config) {
    return new DbAdapter(array(
        "host" => $config->database->host,
        "username" => $config->database->username,
        "password" => $config->database->password,
        "dbname" => $config->database->dbname,
        "charset"   => $config->database->charset
    ));
};

/**
 * If the configuration specify the use of metadata adapter use it or use memory otherwise
 */
$di->set(
    'modelsMetadata',
    function () use ($config) {

        /*if ($config->application->debug) {
            return new MemoryMetaDataAdapter();
        }*/

        return new MetaDataAdapter(array(
            'metaDataDir' => $config->application->cacheDir  . 'metaData/'
        ));

    },
    true
);

/**
 * Start the session the first time some component request the session service
 */
$di['session'] = function () {
    $session = new SessionAdapter();
    $session->start();

    return $session;
};

/**
 * Flash service with custom CSS classes
 */
$di->set('flash', function () {
    return new Flash(array(
        'error' => 'alert alert-danger',
        'success' => 'alert alert-success',
        'notice' => 'alert alert-info'
    ));
});

/**
 * Register the session flash service with the Twitter Bootstrap classes
 */
$di->set(
    'flashSession',
    function () {
        return new Phalcon\Flash\Session(array(
            'error'   => 'alert alert-danger',
            'success' => 'alert alert-success',
            'notice'  => 'alert alert-info',
        ));
    }
);
//Set up the flash session service as flash
/*
$di['flash'] = function(){
    return new Phalcon\Flash\Session(array(
        'error' => 'alert alert-error',
        'success' => 'alert alert-success',
        'notice' => 'alert alert-info'
    ));
};
*/


/**
 * View cache
 */
$di->set(
    'viewCache',
    function () use ($config) {

        if ($config->application->debug) {

            $frontCache = new FrontendNone();
            return new Phalcon\Cache\Backend\Memory($frontCache);

        } else {
            //Cache data for one day by default
            $frontCache = new \Phalcon\Cache\Frontend\Output(array(
                "lifetime" => 86400 * 30
            ));

            return new FileCache($frontCache, array(
                "cacheDir" => APP_DIR . "/apps/cache/views/",
                "prefix"   => "javhd-cache-"
            ));
        }
    }
);

/**
 * Cache
 */
$di->set(
    'modelsCache',
    function () use ($config) {

        if ($config->application->debug) {

            $frontCache = new FrontendNone();
            return new Phalcon\Cache\Backend\Memory($frontCache);

        } else {

            //Cache data for one day by default
            $frontCache = new \Phalcon\Cache\Frontend\Data(array(
                "lifetime" => 86400 * 30
            ));

            return new \Phalcon\Cache\Backend\File($frontCache, array(
                "cacheDir" => APP_DIR . "/apps/cache/data/",
                "prefix"   => "javhd-cache-data-"
            ));
        }
    }
);


$di->set('acl' , function(){
    return new Acl();
});

$di->set('auth' , function(){   
    return new Auth();
});

/**
 * Mail service uses AmazonSES
 */
$di->set('mail', function () {
    return new Mail();
});